package com.example.pixelyear.sql

import android.content.Context
import com.example.pixelyear.R
import com.example.pixelyear.bean.ColorBean
import com.example.pixelyear.utils.UIUtils
import com.socks.library.KLog
import java.lang.ref.WeakReference

class ColorSqlUtils(context: Context) : BaseSqlUtils(context) {

    companion object {
        private lateinit var mInstance: WeakReference<ColorSqlUtils>

        fun getInstance(context: Context): ColorSqlUtils? {
            if (!this::mInstance.isInitialized|| mInstance.get()==null)
                mInstance = WeakReference(ColorSqlUtils(context))
            return mInstance.get()
        }
    }

    fun addColor(id: Int, color: Int, text: String, position: Int): ColorBean? {
        return if (!isExists(id)) {
            KLog.e("yxs", "添加任务：$id")
            write.execSQL(
                "insert into " + Sql.colorName + " (color,msg,position) values(?,?,?)"
                , arrayOf(color.toString(), text, position.toString())
            )
            getFirstData()
        } else {
            write.execSQL(
                "update " + Sql.colorName + " set color=? , msg=?,position=? where _id=?",
                arrayOf(color.toString(), text, position.toString(), id.toString())
            )
            null
        }
    }


    fun addDefault() {
        if (isEmpty()) {
            addColor(-1, UIUtils.getColor(R.color.color_one), "愿你多吃不胖", 0)
            addColor(-1, UIUtils.getColor(R.color.color_two), "愿你深情不负", 1)
            addColor(-1, UIUtils.getColor(R.color.color_three), "愿你傻人傻福", 2)
            addColor(-1, UIUtils.getColor(R.color.color_four), "想要的都拥有", 3)
            addColor(-1, UIUtils.getColor(R.color.color_five), "得不到的都释怀", 4)
        }
    }

    fun getData(): MutableList<ColorBean> {
        val cursor = read.rawQuery(
            "select * from " + Sql.colorName + " order by position asc",
            arrayOf()
        )
        val list = mutableListOf<ColorBean>()
        while (cursor.moveToNext()) {
            val id = cursor.getInt(cursor.getColumnIndex("_id"))
            val color = cursor.getInt(cursor.getColumnIndex("color"))
            val msg = cursor.getString(cursor.getColumnIndex("msg"))
            val bean = ColorBean(color, msg).also {
                it.id = id
            }

            KLog.e("yxs", "查询的任务：" + bean.toString())
            list.add(bean)
        }
        cursor.close()
        return list
    }

    fun delete() {
        write.execSQL("delete from " + Sql.colorName)
    }

    private fun isExists(id: Int): Boolean {
        if (id == -1)
            return false
        val cursor = read.rawQuery(
            "select * from " + Sql.colorName + " where id = ?",
            arrayOf(id.toString())
        )
        val moveToFirst = cursor.moveToFirst()
        cursor.close()
        return moveToFirst
    }


    private fun isEmpty(): Boolean {
        val cursor = read.rawQuery(
            "select * from " + Sql.colorName + "",
            arrayOf()
        )
        val moveToFirst = cursor.moveToFirst()
        cursor.close()
        return !moveToFirst
    }

    private fun getFirstData(): ColorBean? {
        val cursor = read.rawQuery(
            "select * from " + Sql.colorName + "",
            arrayOf()
        )
        val moveToFirst = cursor.moveToFirst()
        if (moveToFirst) {
            val id = cursor.getInt(cursor.getColumnIndex("_id"))
            val color = cursor.getInt(cursor.getColumnIndex("color"))
            val msg = cursor.getString(cursor.getColumnIndex("msg"))
            val bean = ColorBean(color, msg).also {
                it.id = id
            }
            cursor.close()
            return bean
        }
        cursor.close()
        return null
    }


    fun getMax(): Int {
        val cursor = read.rawQuery(
            "select * from " + Sql.colorName + " order by position desc",
            arrayOf()
        )
        cursor.moveToFirst()
        val position = cursor.getInt(cursor.getColumnIndex("position"))
        KLog.e("yxs", "获取的最大数：" + position)
        cursor.close()
        return position
    }

}
